package com.jlnk.androidfmc.fragment.fragments;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jlnk.androidfmc.MainActivity;
import com.jlnk.androidfmc.OrderDetailsActivity;
import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.StoreManagement;
import com.jlnk.androidfmc.entity.CartItem;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;


public class SellerHomeFragment extends Fragment {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public SellerHomeFragment() {
        // Required empty public constructor
    }

    public static SellerHomeFragment newInstance(String param1, String param2) {
        SellerHomeFragment fragment = new SellerHomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }


    }

    // 在 SellerHomeFragment 中
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_seller_home, container, false);

        // 找到ivShopManagementIcon并设置点击事件
        ImageView shopManagementIcon = view.findViewById(R.id.ivShopManagementIcon);
        shopManagementIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击时启动StoreManagement活动
                Intent intent = new Intent(getActivity(), StoreManagement.class);
                startActivity(intent);
            }
        });

        // 找到ivShopImage并设置点击事件
        ImageView shopImage = view.findViewById(R.id.ivShopImage);
        shopImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击时也启动StoreManagement活动
                Intent intent = new Intent(getActivity(), StoreManagement.class);
                startActivity(intent);
            }
        });

        ImageView orderManagementIcon = view.findViewById(R.id.ivOrderManagementIcon);
        orderManagementIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击时启动 OrderDetailsActivity 活动
                Intent intent = new Intent(getActivity(), OrderDetailsActivity.class);

                // 假设以下方法返回有效的订单项列表和总金额
                String orderItemsJson = getSerializedOrderItems();
                double totalAmount = getTotalAmount();

                // 检查数据是否有效
                if (orderItemsJson != null && !orderItemsJson.isEmpty() && totalAmount >= 0) {
                    // 将订单数据和总金额添加到 Intent
                    intent.putExtra("ORDER_ITEMS", orderItemsJson);
                    intent.putExtra("TOTAL_AMOUNT", String.valueOf(totalAmount));
                    startActivity(intent);
                } else {
                    // 处理错误情况，例如显示 Toast 或日志
                    Toast.makeText(getActivity(), "Order data is not available", Toast.LENGTH_SHORT).show();
                    Log.e("SellerHomeFragment", "Order items or total amount is invalid");
                }
            }
        });

        return view;
    }

    // 以下方法应返回有效的订单项 JSON 字符串
    private String getSerializedOrderItems() {
        Gson gson = new Gson();
        // 这里需要您提供实际的订单项列表
        List<CartItem> cartItems = getCartItems(); // 您需要实现这个方法
        Type type = new TypeToken<ArrayList<CartItem>>(){}.getType();
        return gson.toJson(cartItems, type);
    }

    // 以下方法应返回计算的总金额
    private double getTotalAmount() {
        List<CartItem> cartItems = getCartItems(); // 您需要实现这个方法
        double total = 0;
        for (CartItem item : cartItems) {
            total += item.getPrice() * item.getQuantity();
        }
        return total;
    }
    private List<CartItem> getCartItems() {
        // 这里是示例代码，您需要根据实际情况获取订单项
        List<CartItem> cartItems = new ArrayList<>();
        // 例如：
        // cartItems.add(new CartItem("Product Name", 10.0, 1));
        return cartItems;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 通知MainActivity在首页显示"去卖货"按钮和图片
        MainActivity mainActivity = (MainActivity) getActivity();
        if (mainActivity != null) {
            mainActivity.setGoSellVisibility(true);
        }



    }
}